<template>
  <div class='app-backtop' :class="{show}" @click="backtop()">
    <i class="iconfont icon-backtop"></i>
    <p>回到顶部</p>
  </div>
</template>

<script>
export default {
  name: 'app-backtop',
  data () {
    return {
      show: false
    }
  },
  mounted () {
    window.onscroll = () => {
      this.show = document.documentElement.scrollTop > document.documentElement.clientHeight
    }
  },
  methods: {
    backtop () {
      document.documentElement.scrollTop = 0
    }
  }
}
</script>

<style scoped lang='less'>
.app-backtop {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate3d(640px,-50%,0);
  width: 80px;
  height: 80px;
  border-radius: 4px;
  border: 1px solid #e4e4e4;
  background: #fff;
  box-shadow: 0 0 2px #e4e4e4;
  text-align: center;
  padding-top: 10px;
  color: #999;
  opacity: 0;
  transition: opacity 0.6s;
  .icon-backtop {
    font-size: 20px;
  }
  p {
    line-height: 32px;
    font-size: 12px;
  }
  &:hover {
    cursor: pointer;
    color: @xtxColor;
  }
  &.show {
    opacity: 1;
  }
}
</style>
